//
//  KeAnalyzeVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/8.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "KeAnalyzeVC.h"

#import "OneCell.h"
#import "TwoCell.h"
#import "ThreeCell.h"
#import "TodoHttpManager.h"

#import "MJExtension.h"
#import "KeOneModel.h"

#import "SVPullToRefresh.h"
#import "MJRefresh.h"
@interface KeAnalyzeVC ()<UITableViewDelegate,UITableViewDataSource,UTableViewCellDetegate,UTableThreeCellDetegate>

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableDictionary *zongDic;

@property (nonatomic,strong)  KeOneModel *keModel;


@end

@implementation KeAnalyzeVC

-(void)viewWillAppear:(BOOL)animated
{
    //客户分析
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerSecondKeHu:) name:TDManagerSecondKeHu object:nil];
    //客户分析
    [_manager userManagerSecondKeHua];
    
    //客户分析搜索
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerKeFenXiSouSuo:) name:TDManagerKeFenXiSouSuo object:nil];
    
}
//客户分析
-(void)didTDManagerSecondKeHu:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        _zongDic = [notifi.object objectForKey:@"data"];
        //NSLog(@"客户分析的所有数据-----%@",_zongDic);
        _keModel = [KeOneModel mj_objectWithKeyValues:notifi.object[@"data"]];
//        for (FDataOneModel *fdata in _keModel.lhfx.day.data) {
//            NSLog(@"----===========  %@",fdata.name);
//        }
//
        NSMutableArray *countA = [NSMutableArray arrayWithCapacity:0];
        for (FDataOneModel *fdata in self.keModel.lhfx.day.data) {
           // NSLog(@"第二个cell ----===========  %@",fdata.count);
            [countA addObject:fdata.count];
        }
        _countArr = countA;
        NSMutableArray *nameA = [NSMutableArray arrayWithCapacity:0];
        for (FDataOneModel *fdata in self.keModel.lhfx.day.data) {
            //NSLog(@"第二个cell ----===========  %@",fdata.name);
            [nameA addObject:fdata.name];
        }
        _nameArr = nameA;
        
        [self.tabView reloadData];
        
        
    }
}

-(void)didTDManagerKeFenXiSouSuo:(NSNotification *)not
{
    
    _keModel = [KeOneModel mj_objectWithKeyValues:not.object[@"data"]];

    [self.tabView reloadData];
    
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerSecondKeHu object:nil];
    
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerKeFenXiSouSuo object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    
    _countArr = [NSMutableArray arrayWithCapacity:0];
    _nameArr = [NSMutableArray arrayWithCapacity:0];
    
    _zongDic = [NSMutableDictionary dictionary];
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-45-LL_TabbarHeight-LL_StatusBarAndNavigationBarHeight)];
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 300;
    [self.view addSubview:_tabView];
    
    
    
//    __weak KeAnalyzeVC *weakSelf = self;
//
//    [_tabView addPullToRefreshWithActionHandler:^{
//        [weakSelf insertRowAtTop];
//    }];
//
    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(keHuaFenXiRefresh)];
}

-(void)keHuaFenXiRefresh
{
       [_manager userManagerSecondKeHua];
    [self.tabView.mj_header endRefreshing];
}
//#pragma mark--刷新---------刷新--------
//- (void)insertRowAtTop {
//    __weak KeAnalyzeVC *weakSelf = self;
//
//    int64_t delayInSeconds = 2.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        [weakSelf.tabView beginUpdates];
//
//
//        [weakSelf.tabView endUpdates];
//
//        [weakSelf.tabView.pullToRefreshView stopAnimating];
//    });
//}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *ID = @"";
    if (indexPath.row == 0)
    {
       ID = @"CellX";
        OneCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[OneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID allDataModel:_keModel];
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else if(indexPath.row == 1){
        ID = @"CellB";
        
        
        
        TwoCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[TwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID segArray:@[@"日",@"周",@"月",@"季",@"年"] dataModel:_keModel countArr:_countArr nameArr:_nameArr];
        }
        cell.detegate = self;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
        
    }else
    {
        ID = @"CellY";
        ThreeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[ThreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID segArray:@[@"居住区域",@"置业目的",@"需求面积",@"抗性因素"] dataModel:_keModel];
        }
        cell.detegate = self;
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
  
    }
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//TwoCell
-(void)didWithSegmented:(UISegmentedControl *)segmented{
    
    //代理不用  可全部删除代理
}

//ThreeCell
-(void)didThreeWithSegmented:(UISegmentedControl *)segmented{
    //代理不用  可全部删除代理
}




@end
